package com.sohoometer.station.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.nari.coreframe.dao.hibernate3.GenericDaoImpl;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResultWithPageCond;
import com.sohoometer.station.bean.PowerMeterData;
import com.sohoometer.station.bean.PowerMeterInfo;

@Repository("powerMeterDataDao")
public class PowerMeterDataDaoImpl extends GenericDaoImpl<PowerMeterData, String> implements PowerMeterDataDao {
	@Autowired
	public PowerMeterDataDaoImpl(SessionFactory sessionFactory) {
		setSessionFactory(sessionFactory);
	}

	/**
	 * 根据电表表号 查询相应的电表数据,时间根据从早到晚进行排序
	 * @throws Exception 
	 * 
	 * */
	@Override
	public ResultWithPageCond<PowerMeterData> queryPowerMeterDatas(
			String meterNo, List<PowerMeterInfo> meters, Date startQueryTime,
			Date endQueryTime, PageCond pageCond) throws Exception {
		if(meters==null) meters = new ArrayList<PowerMeterInfo>();
			List<String> meterNos = convertToMeterNo(meters);
			if (StringUtils.isNotBlank(meterNo)) {
				meterNos.add(meterNo);
			}
			if(meterNos.size()<1) return null;
		Session currentSession = getCurrentSession();
		Criteria criteria = currentSession.createCriteria(PowerMeterData.class);
		Criteria countCriteria = currentSession.createCriteria(PowerMeterData.class);
//		DetachedCriteria criteria = createDetachedCriteria().add(Restrictions.in("powerMeterNo", list));
		//查询数量
		countCriteria.add(Restrictions.in("powerMeterNo", meterNos));
		  criteria.add(Restrictions.in("powerMeterNo", meterNos));
		  if(startQueryTime!=null&&endQueryTime!=null) {
			  countCriteria.add(Restrictions.between("uploadTime", startQueryTime, endQueryTime));
			  criteria.add(Restrictions.between("uploadTime", startQueryTime, endQueryTime));
			  }
		  criteria.setFirstResult(pageCond.getStart());
		  criteria.setMaxResults(pageCond.getLimit());
		  criteria.addOrder(Order.desc("uploadTime"));
		  countCriteria.setProjection(Projections.rowCount());//投影查询 查询数目
		  Object uniqueResult = countCriteria.uniqueResult();
		  Long i = (Long) uniqueResult;
		  ResultWithPageCond<PowerMeterData> result = new ResultWithPageCond<PowerMeterData>();
		  pageCond.setTotal(i.intValue());
		  //设置数目
		  result.setPageCond(pageCond);
		  result.setList(criteria.list());
		  return result;
	}

	private List<String> convertToMeterNo(List<PowerMeterInfo> meters) {
		List<String>  result = new ArrayList<String>();
		for(PowerMeterInfo info : meters) {
			if(StringUtils.isBlank(info.getPowerMeterNo()))  continue;
			result.add(info.getPowerMeterNo());
		}
		return result;
	}
}
